#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import time
import threading
import win32gui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QWindow
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget, QApplication


class display_external_program(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        t = threading.Thread(target=self.runExe)
        t.start()
        # hwnd = win32gui.FindWindowEx(0, 0, "WindowsForms10.Window.8.app.0.378734a", "VISSIM 4.30-01 - (Untitled)")
        hwnd = win32gui.FindWindowEx(0, 0, "notepad", "test.txt - 记事本")
        start = time.time()
        while hwnd == 0:
            # hwnd = win32gui.FindWindowEx(0, 0, "WindowsForms10.Window.8.app.0.378734a", "VISSIM 4.30-01 - (Untitled)")
            hwnd = win32gui.FindWindowEx(0, 0, "notepad", "test.txt - 记事本")
            end = time.time()
            if end - start > 5:
                return
        external_window = QWindow.fromWinId(hwnd)
        external_window = QWidget.createWindowContainer(external_window, self)
        self.setCentralWidget(external_window)
        # self.setWindowFlags(Qt.FramelessWindowHint)
        # self.setGeometry(300, 300, 400, 300)

    @staticmethod
    def runExe():
        # path = "E:/Users/lishan/Desktop/vissim.lnk"
        path = "E:/Users/lishan/Desktop/test.txt"
        os.startfile(path)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = display_external_program()
    ex.show()
    sys.exit(app.exec_())
